/*
 * Copyright 2010-2012 Susanta Tewari. <freecode4susant@users.sourceforge.net>
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

package coalescent.cef.command.listener;

import cef.command.CommandI;
import cef.command.Model_MLE_Command;
import cef.command.listener.*;
import coalescent.data.K69Data;
import coalescent.model.K69;
import commons.util.PrintUtil;
import org.jfree.chart.ChartPanel;
import org.jfree.chart.JFreeChart;
import org.jfree.chart.axis.NumberAxis;
import org.jfree.chart.plot.XYPlot;
import org.jfree.chart.renderer.xy.XYSplineRenderer;
import org.jfree.data.xy.XYSeries;
import org.jfree.data.xy.XYSeriesCollection;

import java.math.BigDecimal;
import java.util.Map;

import static commons.util.FormatUtil.format;

/**
 * Contains a number of output listener implementations for MLE under K69.
 *
 * @since
 * @version
 * @author Susanta Tewari
 * @history Created on 2/22/13.
 */
public class K69_MLE_OLs {

    /**
     * @version version
     * @since 1.4.0
     * @history Created on 02/22/2013
     * @author Susanta Tewari
     */
    public static class OC extends FigureEditorOutputComponent {

        @Override
        public void initComponents(final CommandOutputEvent event)
                throws CommandOutputListenerException {

            final Model_MLE_Command<K69, K69Data> command = (Model_MLE_Command) event.getSource();

            title = "Mutation Rate MLE under Infinite Sites Model";

            final XYSeries xyseries                = new XYSeries(title);
            final Map<K69, BigDecimal> theta_probs = command.getLHoods();

            for (final K69 m : theta_probs.keySet()) {
                xyseries.add(m.getMutationRate(), theta_probs.get(m));
            }

            final NumberAxis x_axis = new NumberAxis("Theta");
            final NumberAxis y_axis = new NumberAxis("Probability");

            x_axis.setAutoRangeIncludesZero(false);
            y_axis.setAutoRangeIncludesZero(false);

            final XYSplineRenderer xysplinerenderer = new XYSplineRenderer();
            final XYPlot xyplot = new XYPlot(new XYSeriesCollection(xyseries), x_axis, y_axis,
                                             xysplinerenderer);
            final JFreeChart chart = new JFreeChart(title, JFreeChart.DEFAULT_TITLE_FONT, xyplot,
                                         true);

            figure = new ChartPanel(chart);
        }

        @Override
        public CommandOutputListener newInstance() {
            return new OC();
        }

        @Override
        public Class<? extends CommandI> getTargetCommandClass() {
            return Model_MLE_Command.class;
        }
    }

    /**
     * @version version
     * @since 1.4.0
     * @history Created on 02/22/2013
     * @author Susanta Tewari
     */
    public static class PW extends CommandPrintWriter {

        @Override
        public void receivedEventImpl(final CommandOutputEvent cmdOutputEvent)
                throws CommandOutputListenerException {

            final Model_MLE_Command<K69, K69Data> cmd =
                (Model_MLE_Command) cmdOutputEvent.getSource();
            final K69 mle = cmd.getMLE();

            getPrintWriter().println("MLE: " + format(mle.getMutationRate()));
            getPrintWriter().println("\n");
            getPrintWriter().println(PrintUtil.print2D(getOutputTable(cmd.getLHoods()), 2, 1,
                    null));
            getPrintWriter().flush();
        }

        private String[][] getOutputTable(final Map<K69, BigDecimal> lHoods) {

            final String[][] outputTable = new String[lHoods.size() + 1][2];

            outputTable[0] = new String[] { "Mutation Rate", "LHood" };

            int i1 = 0;

            for (final K69 m : lHoods.keySet()) {

                i1++;

                final String param = format(m.getMutationRate());
                final String lhood = format(lHoods.get(m));

                outputTable[i1] = new String[] { param, lhood };
            }

            return outputTable;
        }

        @Override
        public CommandOutputListener newInstance() {
            return new PW();
        }

        @Override
        public Class<? extends CommandI> getTargetCommandClass() {
            return Model_MLE_Command.class;
        }
    }
}
